<?php
namespace app\install\controller;
use think\Controller;
class Index extends Controller{
    public function _initialize(){
    	$this->lock = CONF_PATH.request()->module().'/install.lock';
        if (is_file($this->lock)) { $this->redirect('/');}
        $this->assign('namespace', ns());
    }
    public function license(){ return $this->fetch();}
    public function index(){
        $step = request()->request('step');
        if (is_numeric($step)) {
            $this->assign('steps', ['安装协议', '环境检查', '站点配置', '系统安装', '安装成功']);
            $this->step = $step;
            $step = 'step' . $step;
            $this->tpl = $step;
            $this->success('', '', $this->$step());
        } else {
            return $this->fetch();
        }
    }
    protected function step0(){
        if (request()->isGet()) { return $this->fetch($this->tpl);}
    }

    protected function step1(){
        if (request()->isGet()) {
            \think\Session::set('error', 0);
            $this->assign('check_env', check_env());
            $this->assign('check_dirfile', check_dirfile());
            $this->assign('check_func', check_func());
            $this->assign('error', \think\Session::get('error'));
            return $this->fetch($this->tpl);
        }
    }

    protected function step2()
    {
        if (request()->isGet()) {
            return $this->fetch($this->tpl);
        } elseif (request()->isPost()) {
            $input = input();
           
            $dbconfig = array(
                'type' => 'mysql',
                'hostname' => input('hostname','',null),
                'database' => input('database','',null),
                'hostport' => $input['hostport'],
                'username' => input('username','',null),
                'password' => input('password','',null),
                'params' => [],
                'charset' => 'utf8',
                'prefix' => $input['prefix'],
            );
            $db = \think\Db::connect($dbconfig);
            $sql1 = "CREATE TABLE IF NOT EXISTS `" . $dbconfig['prefix'] . 'test' . "` (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(id),ebcmstest varchar(15));";
            $sql2 = "DROP TABLE IF EXISTS `" . $dbconfig['prefix'] . 'test' . "`;";
            if (0 != $db->execute($sql1) || 0 != $db->execute($sql2)) {
                $this->error('数据库配置错误！');
            }
            $dbdemo = $input['demodatabase'];
            \think\Session::set('dbdemo', $dbdemo);
            \think\Session::set('dbconfig', $dbconfig);
            \think\Session::set('manager_email', $input['manager_email']);
            $this->success('配置成功！');
        }
    }

    protected function step3()
    {
        if (request()->isGet()) {
            return $this->fetch($this->tpl);
        } elseif (request()->isPost()) {
            $process = input('process');
            $dbconfig = \think\Session::get('dbconfig');
            switch ($process) {
                case 'createdatabase':
                    if (!$dbconfig) {
                        $this->error('数据库配置错误！');
                    }
                    $tables = '安装数据...<br>';
                    if (\think\Session::get('dbdemo')) {
                        $tables .= '载入演示数据库...<br>';
                        $sql = file_get_contents(APP_PATH . 'install/data/install_demo.sql');
                    } else {
                        $tables .= '载入数据库...<br>';
                        $sql = file_get_contents(APP_PATH . 'install/data/install.sql');
                    }
                    $sql = str_replace('ky_', $dbconfig['prefix'], $sql);
                    $ret = array();
                    $num = 0;
                    $sqls = explode(";" . PHP_EOL, \kykkk\Func::streol(trim($sql)));
                    foreach ($sqls as $query) {
                        $ret[$num] = '';
                        $queries = explode(PHP_EOL, trim($query));
                        foreach ($queries as $v) {
                            $ret[$num] .= (isset($v[0]) && $v[0] == '#') || (isset($v[1]) && isset($v[1]) && $v[0] . $v[1] == '--') ? '' : $v;
                        }
                        $num++;
                    }
                    $tables .= '创建数据表...<br>';
                    $db = \think\Db::connect($dbconfig);
                    foreach ($ret as $key => $value) {
                        if ($value) {
                            $db->execute($value);
                            if (substr($value, 0, 12) == 'CREATE TABLE') {
                                $tables .= '创建数据表...' . preg_replace("/CREATE TABLE `?([a-z0-9_]+)`? .*/is", "\\1", $value) . '<br>';
                            }
                        }
                    }
                    $tables .= '创建数据库...完成!';
                    $result = array(
                        'info' => $tables,
                        'process' => 'updatedbconfig',
                    );
                    return $result;
                    break;

                case 'updatedbconfig':
                    if (!$dbconfig) {
                        $this->error('数据库配置错误！');
                    }
                    $info = '更新系统配置...<br>';
                   
                  
                    // 更新数据库配置文件
                    $dbconfig_old = file_get_contents(APP_PATH . 'install/data/database.php');
                    $dbconfig = str_preg_parse($dbconfig_old, $dbconfig, true);
                    file_put_contents(CONF_PATH . 'database.php', $dbconfig);
                  

                    $info .= '更新系统配置...完成！<br>';

                    \kykkk\Func::deldir(TEMP_PATH);
                    \kykkk\Func::deldir(LOG_PATH);
                    \kykkk\Func::deldir(CACHE_PATH);
                    $info .= '删除缓存！<br>';
                    	
                    file_put_contents($this->lock, '#' . date('Y-m-d H:i:s'));
                    $info .= '生成安全文件！<br>';
                    $info .= '安装成功！';

                    $result = array(
                        'info' => $info,
                        'process' => 'end',
                    );
                    return $result;
                    break;
            }
        }
    }

}